#include <iostream>

#include "Databank.hpp"
#include "Item.hpp"

Item::Item(Actor* owner, Zone* zone, char shape, EObjectSize size, std::string name, bool transparent, Vector2d position, TCODColor fore, TCODColor back) : 
	GameObject(name, shape, position, size, transparent, fore, back), Id(0), Owner(owner), Area(zone)
{
	Properties.resize(EIT_COUNT, NULL);

	Mat = Material::getByName(Databank::ItemBank[name]->PossibleMaterials[Utils::random0ToN(Databank::ItemBank[name]->PossibleMaterials.size())]);
	if (Material::data[Mat].isBurnable())
		Properties[EIT_BURNABLE] = new PropertyBurning(this, Material::data[Mat].Burnable.BurnTimer, Material::data[Mat].Burnable.Flammability, Material::data[Mat].Burnable.Spreads);
}

Item::~Item()
{
	Properties.clear();
}

unsigned int Item::getId()
{
	return Id;
}

Actor* Item::getOwner()
{
	return Owner;
}

ItemProperty* Item::getProperty(EItemType type)
{
	return Properties[type];
}

Zone* Item::getZone()
{
	return Area;
}

bool Item::isProperty(EItemType type)
{
	if (Properties[type])
		return true;

	return false;
}

void Item::onBurn()
{
	if (Properties[EIT_BURNABLE])
		Properties[EIT_BURNABLE]->action();
	else
		std::cout<<"You can't burn that."<<std::endl;
}

void Item::onDrink()
{
	std::cout<<"You can't drink that."<<std::endl;
}

void Item::onDrop()
{
	std::cout<<"You can't drop that."<<std::endl;
}

void Item::onExamine()
{
	std::cout<<"You can't examine that."<<std::endl;
}

void Item::onGet()
{
	std::cout<<"You can't get that."<<std::endl;
}

void Item::onLook()
{
	std::cout<<"You can't look at that."<<std::endl;
}

void Item::onRead()
{
	std::cout<<"You can't read that."<<std::endl;
}

void Item::onUse()
{
	std::cout<<"You can't use that."<<std::endl;
}

void Item::onWield()
{
	std::cout<<"You can't wield that."<<std::endl;
}

void Item::setId(unsigned int id)
{
	Id = id;
}

void Item::update()
{
	for (unsigned int i = 0; i < EIT_COUNT; ++i)
		if (Properties[i])
			Properties[i]->update();
}

Item* Item::addItem(std::string name, Vector2d pos, Zone* area, Actor* owner)
{
	if (Databank::ItemBank.find(name) != Databank::ItemBank.end())
		return new Item(owner, area, Databank::ItemBank[name]->Shape, Databank::ItemBank[name]->Size, 
		name, Databank::ItemBank[name]->Transparent, pos, Databank::ItemBank[name]->ForeCol, Databank::ItemBank[name]->BackCol);

	return NULL;
}